package com.yyge.dto;

import lombok.Data;

import java.io.Serializable;

/**
* 返回值类型的固定成员：
*  Integer code  记录当前的状态码   0 代表成功  1 代表错误  2....
*  String message 要返回的简单的字符串信息
*  Object data  其他的复杂数据类型
*/
@Data
public class R<T> implements Serializable {
   private Integer code ;
   private String msg ;
   private T data ;
   // 屏蔽默认构造函数，让外界不不能直接创建对象
   private R(){}
   public static <T> R<T> ok (){
     final R<T> r = new R<> ();
     r.code = ResultCode.SUCCESS.code ;
     r.msg = ResultCode.SUCCESS.message ;
     return r;
  }

   public static <T> R<T> ok (T data ){
     final R<T> r = new R<> ();
     r.code = ResultCode .SUCCESS .code ;
     r.msg = ResultCode .SUCCESS .message ;
     r.data = data ;
     return r;
  }

   public static <T> R<T> ok (ResultCode resultCode , T data ) {
    final R<T> r = new R<>();
    r.code = resultCode.code;
    r.msg = resultCode.message;
    r.data = data;
    return r;
  }

  public static <T> R<T> ok (String message , T data ){
     final R<T> r = new R<> ();
     r.code = ResultCode .SUCCESS .code ;
     r.msg = message ;
     r.data = data ;
     return r;
  }
   public static <T> R<T> error (){
     final R<T> r = new R<> ();
     r.code = ResultCode .ERROR .code ;
     r.msg = ResultCode .ERROR .message ;
     return r;
  }

   public static <T> R<T> error (ResultCode resultCode ){
     final R<T> r = new R<> ();
     r.code = resultCode .code ;
     r.msg = resultCode .message ;
     return r;
  }

   public static <T> R<T> error (ResultCode resultCode ,T data ){
     final R<T> r = new R<> ();
     r.code = resultCode .code ;
     r.msg = resultCode .message ;
     r.data = data ;
     return r;
  }

}